package java_thread;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class demo_17_lock {
    public static void main(String[] args) {
        Executor executor = Executors.newFixedThreadPool(10);
        for (int i=0;i<10;i++){
            executor.execute(new testRunnable());
        }
    }
}

class  testRunnable implements Runnable{
    static int i = 0;
    static Lock lock = new ReentrantLock();
    @Override
    public void run() {
        while (1==1){
            try {

                lock.lock();
                System.out.print("取得："+i);
                Thread.sleep(100);
                System.out.println("__追加："+(++i));
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}
